package com.example.lock.util;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * <p>Description: </p>
 *
 * @author zg
 * @version v1.0.0
 * @since 2024/8/20
 **/
@Slf4j
@RequiredArgsConstructor
@Component
public class RedissonLockUtils {

    private final RedissonClient redissonClient;


    public RLock lock(String lockKey) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock();
        return lock;
    }

    public RLock lock(String lockKey, int leaseTime) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock(leaseTime, TimeUnit.SECONDS);
        return lock;
    }

    public RLock lock(String lockKey, int leaseTime, boolean isFairLock) {
        RLock lock = isFairLock ? redissonClient.getFairLock(lockKey) : redissonClient.getLock(lockKey);
        lock.lock(leaseTime, TimeUnit.SECONDS);
        return lock;
    }

    public RLock lock(String lockKey, TimeUnit unit, int timeout) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock(timeout, unit);
        return lock;
    }

    /**
     * 尝试获得锁
     * @param lockKey
     * @param waitTime 等待时长
     * @param leaseTime 加锁时长
     * @param unit
     * @return
     */
    public boolean tryLock(String lockKey, int waitTime, int leaseTime, TimeUnit unit) {
        RLock lock = redissonClient.getLock(lockKey);
        try {
            return lock.tryLock(waitTime, leaseTime, unit);
        } catch (InterruptedException e) {
            return false;
        }
    }

    /**
     * 尝试获得锁
     * @param lockKey
     * @param waitTime 等待时长(秒)
     * @param leaseTime 加锁时长(秒)
     * @return
     */
    public boolean tryLock(String lockKey, int waitTime, int leaseTime) {
        RLock lock = redissonClient.getLock(lockKey);
        try {
            return lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            return false;
        }
    }
    /**
     * 尝试获得锁
     * @param lockKey
     * @param waitTime 等待时长(秒)
     * @param leaseTime 加锁时长(秒)
     * @param isFairLock
     * @return
     */
    public RLock tryLock(String lockKey, int waitTime, int leaseTime, boolean isFairLock) throws Exception {
        RLock lock = isFairLock ? redissonClient.getFairLock(lockKey) : redissonClient.getLock(lockKey);
        if(!lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS)){
            log.error("[{}]获取锁失败，lockKey={}, isFairLock={},waitTime={},leaseTime={}", Thread.currentThread().getName(), lockKey, isFairLock, waitTime, leaseTime);
            throw new Exception("获取锁失败，lockKey=" + lockKey);
        }
        return lock;
    }

    public RLock tryLockAndGet(String lockKey, int waitTime, int leaseTime) throws Exception {
        RLock lock = redissonClient.getLock(lockKey);
        if (!lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS)) {
            log.error("[{}]获取锁失败，lockKey={},waitTime={},leaseTime={}", Thread.currentThread().getName(), lockKey, waitTime, leaseTime);
            throw new Exception("获取锁失败，lockKey=" + lockKey);
        }
        return lock;
    }

    /**
     * 尝试获得锁(加锁时长不限制)
     * @param lockKey
     * @param waitTime 等待时长(秒)
     * @return
     */
    public boolean tryLock(String lockKey, int waitTime) {
        RLock lock = redissonClient.getLock(lockKey);
        try {
            return lock.tryLock(waitTime, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            return false;
        }
    }

    /**
     * 尝试获得锁 (加锁时长不限制)
     * @param lockKey
     * @param waitTime 等待时长
     * @param unit
     * @return
     */
    public boolean tryLock(String lockKey, int waitTime, TimeUnit unit) {
        RLock lock = redissonClient.getLock(lockKey);
        try {
            return lock.tryLock(waitTime, unit);
        } catch (InterruptedException e) {
            return false;
        }
    }

    public RLock getLock(String lockKey){
        return redissonClient.getLock(lockKey);
    }

    public boolean tryLock(RLock lock, int waitTime){
        try {
            return lock.tryLock(waitTime, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void unlock(String lockKey) {
        RLock lock = redissonClient.getLock(lockKey);
        unlock(lock);
    }

    public void unlock(RLock lock) {
        if(lock != null && lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }
}
